<?php
$pageTitle = "用户管理";
include 'header.php';

// 获取所有用户
try {
    $stmt = $pdo->query('SELECT id, username, school, created_at FROM users ORDER BY id');
    $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die('数据库查询失败: ' . $e->getMessage());
}
?>
<div class="row">
    <h1 class="mb-4">用户管理</h1>
    
    <?php if (isset($_GET['status']) && $_GET['status'] === 'created'): ?>
        <div class="alert alert-success">
            用户创建成功！
        </div>
    <?php endif; ?>

    <div class="d-flex justify-content-between mb-3">
        <h2>用户列表</h2>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createUserModal">创建新用户</button>
    </div>

    <div class="table-responsive">
        <table class="table table-striped table-hover table-bordered">
            <thead>
                <tr>
                    <th>#</th>
                    <th>用户名</th>
                    <th>单位</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($users as $index => $user): ?>
                    <tr>
                        <td><?= htmlspecialchars($index + 1) ?></td>
                        <td><?= htmlspecialchars($user['username']) ?></td>
                        <td><?= htmlspecialchars($user['school']) ?></td>
                        <td><?= htmlspecialchars($user['created_at']) ?></td>
                        <td>
                        <?php if ($_SESSION['user_id']!=$user['id']){ ?>
                            <a href="user_edit.php?id=<?= $user['id'] ?>" class="btn btn-sm btn-warning">编辑</a>
                            <a href="user_delete.php?id=<?= $user['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('确定要删除这个用户吗？')">删除</a>
                        <?php } ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- 创建用户模态框 -->
<div class="modal fade" id="createUserModal" tabindex="-1" aria-labelledby="createUserModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <form class="modal-content" action="user_create.php" method="post">
            <div class="modal-header">
                <h5 class="modal-title" id="createUserModalLabel">创建新用户</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <label for="username" class="form-label">用户名</label>
                    <input type="text" class="form-control" id="username" name="username" required>
                </div>
                <div class="mb-3">
                    <label for="school" class="form-label">学校</label>
                    <input type="text" class="form-control" id="school" name="school" required>
                </div>
                <div class="mb-3">
                    <label for="password" class="form-label">密码</label>
                    <input type="password" class="form-control" id="password" name="password" required>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                <button type="submit" class="btn btn-primary">创建用户</button>
            </div>
        </form>
    </div>
</div>

<?php include 'footer.php'; ?>